package com.lu.dog.dbrowser.ui.webview;

import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

/**
 * Created by hechuan on 2017/12/20.
 */

public class CustomWebViewClient extends WebChromeClient{
    private IComponent component;

    public CustomWebViewClient(IComponent component) {
        super();
        this.component = component;
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        // TODO Auto-generated method stub
        //return super.onJsAlert(view, url, message, result);

        if(component != null){
            component.showDialog(message, false);

            result.confirm();

            return true;
        }

        return super.onJsAlert(view, url, message, result);
    }


    @Override
    public boolean onJsConfirm(WebView view, String url, String message,
                               final JsResult result) {
        // TODO Auto-generated method stub

        if(component != null){
            component.showNegtiveDialog(message, false, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    result.confirm();
                }
            }, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    result.cancel();
                }
            });

            return true;
        }

        return super.onJsConfirm(view, url, message, result);
    }

}
